﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace EDTS.Utils
{
    public class LogHelper
    {
        private static object errorLog = new object();
        private static object infoLog = new object();
        /// <summary>
        /// 保存出错日志
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="path"></param>
        public static void SaveErrorLog(Exception ex,string path)
        {
            try
            {
                lock (errorLog)
                {
                    string filename = path + DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                    StreamWriter sw = new StreamWriter(filename, true, System.Text.Encoding.UTF8);
                    string errTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss:fff");
                    sw.WriteLine("--------------" + errTime + "----------------");
                    sw.WriteLine("消息：" + ex.Message);
                    sw.WriteLine("错误类型：" + ex.GetType().ToString());
                    sw.WriteLine(ex.StackTrace);
                    sw.WriteLine("--------------" + errTime + "----------------\r\n\r\n");
                    sw.Flush();
                    sw.Close();
                }
            }
            catch { }
        }

        /// <summary>
        /// 保存信息输出
        /// </summary>
        /// <param name="ex"></param>
        /// <param name="path"></param>
        public static void SaveInfoLog(string info, string filename)
        {
            try
            {
                lock (infoLog)
                {
                    StreamWriter sw = new StreamWriter(filename, true, System.Text.Encoding.UTF8);
                    sw.WriteLine(info);
                    sw.Flush();
                    sw.Close();
                }
            }
            catch { }
        }
    }
}
